package stz.flash.option.strobe;

import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import stz.flash.option.OptionLabel;
import stz.flash.string.OptionInformation;
import stz.flash.string.OptionString;
import blackberry.util.field.DjChoiceField;
import blackberry.util.field.DjLabelField;

public class StrobeOnTimeManager extends DjChoiceField implements FieldChangeListener{

	private static final int ARRAY_LENGTH = 3;

	public static final Integer INTEGER_OF_ON_TIME1 = new Integer(10);
	public static final Integer INTEGER_OF_ON_TIME2 = new Integer(30);
	public static final Integer INTEGER_OF_ON_TIME3 = new Integer(50);
	
	public static final Vector choicesVector = new Vector(ARRAY_LENGTH);
	private static final Hashtable table = new Hashtable(ARRAY_LENGTH); 
	
	public StrobeOnTimeManager() {
		super(DjLabelField.createSimpleLabel(OptionString.ON_TIME_MANAGER_CHECK_BOX_LABEL), 
				OptionLabel.createInfoLabel(OptionInformation.ON_TIME_MANAGER_CHECKBOX_INFOREMATION),
				null);
		setVectorAndTable();
		setChoices();
		initSelectedIndex();
		setChangeListener(this);
	}
	private void setVectorAndTable() {
		choicesVector.addElement("약");
		choicesVector.addElement("중");
		choicesVector.addElement("강");
		table.put(choicesVector.elementAt(0), INTEGER_OF_ON_TIME1);
		table.put(choicesVector.elementAt(1), INTEGER_OF_ON_TIME2);
		table.put(choicesVector.elementAt(2), INTEGER_OF_ON_TIME3);
	}
	private void setChoices() {
		String[] choices = new String[table.size()];
		for (int i = 0; i < ARRAY_LENGTH; i++) {
			choices[i] = (String) choicesVector.elementAt(i);
		}
		setChoices(choices);
	}
	private void initSelectedIndex() {
		for (int i = 0; i < ARRAY_LENGTH; i++) {
			if (table.get(choicesVector.elementAt(i)).equals(
					StrobeOption.getIns().getOnTime())) {
				setSelectedIndex(i);
				return;
			}
		}
	}
	
	public void fieldChanged(Field field, int context) {
		if (context == 0 || context == PROGRAMMATIC)
			return;
		StrobeOption.getIns().setOnTime(
				(Integer) table.get(getChoice(getSelectedIndex())));
	}
}
